package com.smart.core;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class FileMaker {
	public FileMaker fm;
	public FileWriter fw;
	public File f;
	public Document doc;
	public Element elem;
	public String fi;
	public FileWriter fileWriter;
	public File file;
	public Element theElem;

	FileMaker() {
	}

	File getFile(String filename) {
		File f1 = new File(filename);
		try {
			 System.out.println("in getFile");
			if (!(new File(f1.getCanonicalPath()).exists())) {
				 System.out.println("getFile Making new file");
				file = new File(filename);
				return file;
			} else {
				file = f1.getCanonicalFile();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return file;
	}

	Document getDocument(File file, String rootName) {
		doc = new Document();
		SAXBuilder sxb = new SAXBuilder();
		if (file.length() > 10) {
			try {
				doc = sxb.build(file.getCanonicalPath());
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			Element elem = new Element(rootName);
			doc.setRootElement(elem);
		}
		// file.delete(); < Why did i do this? for test only???
		return doc;
	}

	Boolean scanDoc(String tag) {
		List<Element> allChildren = doc.getRootElement().getChildren();
		Element current = null;
		Iterator<Element> i = allChildren.iterator();
		while (i.hasNext()) {
			current = (Element) i.next();
			if (current.getAttribute(tag).equals(tag)) {
				return true;
			}

			return true;
		}
		return true;
	}

	boolean writeToFile(Document doc, Element elem, String data, String txt) {
		boolean retval = false;
		if (!checkContent(doc, elem.getName())) {
			elem.addContent(new Element(data).setText(txt));
			if (doc.getRootElement() != null) {
				doc.getRootElement().addContent(elem);
				retval = true;
			}
		} else {
			Element telem = elem;
			doc.getRootElement().removeChildren(elem.getName());
			telem.addContent(new Element(data).setText(txt));
			doc.getRootElement().addContent(elem);
			retval = true;
		}
		return retval;
	}

	Element getElement(Document doc, String pElement, boolean getData,
			Element theElem) {
		Element retval = null;
		List<Element> allChildren;
		if (doc != null) {
			allChildren = doc.getRootElement().getChildren();
			Iterator<Element> i = allChildren.iterator();
			// System.out.println("input:" + pElement);
			while (i.hasNext()) {
				retval = (Element) i.next();
				// System.out.println("i=" + retval.getName());
				if (retval.getName().equals(pElement)) {
					return retval;
				}
			}
		}
		return retval;
	}

	boolean checkContent(Document doc, String content) {
		boolean retval = false;
		List<Element> allChildren;
		if (doc != null) {
			allChildren = doc.getRootElement().getChildren();
			Element current = null;
			Iterator<Element> i = allChildren.iterator();
			// System.out.println("input:" + content);
			while (i.hasNext()) {
				current = (Element) i.next();
				// System.out.println("i=" + current.getName());
				if (current.getName().equals(content)) {
					retval = true;
				}
			}
		}
		return retval;
	}

	void outputXML(Document doc, File file) {
		XMLOutputter xmlOutput = new XMLOutputter();
		xmlOutput.setFormat(Format.getPrettyFormat());
		StringWriter out = new StringWriter();
		try {
			xmlOutput.output(doc, out);
			// System.out.println("output xml success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			FileUtils.writeStringToFile(file, out.toString(), false);
			// System.out.println("write string to file sucessful");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getText(Document doc, String elemName) {
		String retval = null;
		List<Element> allChildren = doc.getRootElement().getChildren();
		Element current = null;
		Iterator<Element> i = allChildren.iterator();
		while (i.hasNext()) {
			current = (Element) i.next();
			if (current.getName().equals(elemName)) {
				retval = current.getValue().trim();
				break;
			}
		}
		return retval;

	}
}
